var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'order_entry_ajs.html', controller: ListCustCtrl}).
       when('/deletetran', {templateUrl: 'order_entry_ajs.html', controller: ListCustCtrl}).
      when('/edittran/', {templateUrl: 'order_entry_ajs.html', controller: ListCustCtrl}). 
      when('/updtorder/', {templateUrl: 'order_entry_ajs.html', controller: ListCustCtrl}). 
      when('/cancel/', {templateUrl: 'order_entry_ajs.html', controller: ListCustCtrl}). 
      otherwise({redirectTo: '/'});
}]);


app.service('ContactService', function () {
   //  $window.alert("Hello Details  "+orderid);
    this.get = function (tranid,$window) {
        for (i in tranentrymast) {
            if (tranentrymast[i].tranid == tranid) {
                 $window.alert("Edit Cancellation Details  "+tranid);
                return tranentrymast[i];
            }
        }
 
    };
}
);


function ListCustCtrl($scope, $http,$window,ContactService,$location)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 75;
	$scope.Math = window.Math;
	$scope.newtranentrymast = {};
	
	$scope.paymode = [{ id: 1, name: 'CASH'}, { id: 2, name: 'CHEQUE'} ];
	
	
	 $http.get('customerdata2.php').success(function(data2) {
       $scope.custmast = data2;
	  }); 
	
    $http.get('tranmastdata.php').success(function(data) {
    $scope.tranentrymast = data;
    });
    
     $http.get('customerdata.php').success(function(data1) {
		$scope.customermast = data1;
	  }); 
	 
//$scope.t=1;	 
//	   if $scope.t == 1 {
//		var sql = 'customerdata2.php';
//			$window.alert(sql);   
  $scope.custmast = ["SANDEEP B", "SANA INTERNATIONAL TRAVELS", "KARRM INFRASTRUCTURE PVT LTD", "S R TRAVELS", "YOGI TOURS TRAVELS", "KHEMSONS HOLIDAYS", "AERTRIP", "PRASHANT SHENDE", "MANVENDRA SHAHI", "PALAKONLINE" ]; 
	//$scope.custmast = angular.copy($scope.custmast1);
//	};
  
    
	
	$scope.edit = function(tranid) {
     
//	$window.alert("Edit Customer Details  "+tranid);
      // var i =0;
          for(i in $scope.tranentrymast) {
          // $window.alert("outer value of i is "+i);
            if($scope.tranentrymast[i].tranid == tranid)
            {
               //  $window.alert(" value of orderid is "+i+ "  "+orderid);
               //we use angular.copy() method to create copy of original object
               $scope.newtranentrymast = angular.copy($scope.tranentrymast[i]);
             //  $window.alert(" value of no of pax is "+No_of_Pax+ "  "+cantrndetid);
            }
        }
        
    };
    
   //Function to Delete Record in the Expense Master based on the exptranid 
   $scope.deletetran = function(item) {
      if (confirm('Do you really want to delete this record?')) 
        {
            $window.alert(item+' Record is being Deleted');
			var sql = 'tran_order_deletedata.php?orderid='+item;
			$window.alert(sql);
            $http.post(sql).success(function(data)
            {
                $window.alert( 'Record for ' + item+ 'got deleted Successfully');
                 $scope.activePath = $location.path('tran_order_ajs.php#/editorder/');
                 window.location.href = '#/editorder/';
            });
                //window.location.href = 'customer_search_ajs.php';
        }
      };
      
	   
	  
	  
    $scope.updtorderdata = function(newtranentrymast) 
  {   
  
     $window.alert("Updation of Record for "+newtranentrymast.trandate + ","+ newtranentrymast.clist + ","+ newtranentrymast.amtreceived + ","+ newtranentrymast.paymode + 
						","+ newtranentrymast.drawnon+ ","+ newtranentrymast.paydate+ ","+ newtranentrymast.onactoff+ ","+ newtranentrymast.contactnumber + 
						","+ newtranentrymast.fromdate + ","+ newtranentrymast.todate +	","+ newtranentrymast.remarks);
	 
	 var sql = 'order_entry_ajs_update_data.php?'+'trandate='+newtranentrymast.trandate +'&clist='+newtranentrymast.clist
											 +'&amtreceived='+newtranentrymast.amtreceived +'&paymode='+newtranentrymast.paymode
											 +'&drawnon='+newtranentrymast.drawnon +'&paydate='+newtranentrymast.paydate
											 +'&onactoff='+newtranentrymast.onactoff +'&contactnumber='+newtranentrymast.contactnumber
											 +'&fromdate='+newtranentrymast.fromdate +'&remarks='+newtranentrymast.remarks
											 +'&todate='+newtranentrymast.todate +'&tranid='+newtranentrymast.tranid;
	 
	 $window.alert(sql);
	 
     $http.post(sql).success(function(data)  
   {
     $window.alert( 'Record for Tran id '+ newtranentrymast.tranid  + ' and Tran Date ' + newtranentrymast.trandate + 'got Successfully Updated');
     window.location.href = 'order_entry_ajs.php#/edittran/';
   });
  };
      
	   //Function to Insert Data in the Expense Master  
	   $scope.saveorderdata = function(newordertranmast) 
	  	{   
            $window.alert("Save Record for "+newordertranmast.orderdate +","+newordertranmast.customername + ","+ newordertranmast.contactnumber + ","+ newordertranmast.deliverydate+ ","+ newordertranmast.soldby+ ","+ newordertranmast.deliveredby+ ","+ newordertranmast.glassfrom  );
			var sql = 'tran_order_insertdata.php?'+'orderdate='+newordertranmast.orderdate+'&refnumber='+newordertranmast.refnumber+'&customername='+newordertranmast.customername +'&contactnumber='+newordertranmast.contactnumber+'&deliverydate='+newordertranmast.deliverydate+'&soldby='+newordertranmast.soldby+'&deliveredby='+newordertranmast.deliveredby+'&glassfrom='+newordertranmast.glassfrom+'&note='+newordertranmast.note+'&remarks='+newordertranmast.remarks+'&grandtotal='+newordertranmast.grandtotal+'&advreceived='+newordertranmast.advreceived;
			$window.alert(sql);
			$http.post(sql).success(function(data)  
			{
				$window.alert( 'Record for '+  newordertranmast.customername + "  ,  " + newordertranmast.orderdate+ 'got Successfully Saved');
				 window.location.href = 'tran_order_ajs.php#/editorder/';
			});
		};
	  	  
	  
	  
	   //Function to Clear the selected parameters in the search screen.
	   $scope.cancel= function()
		{
        $scope.activePath = null;
		//$window.alert( 'Cancel ');
		$scope.activePath = $location.path('tran_order_ajs.php#/editorder/');
		window.location.href = '#/editorder/';
		};
      
 }
    
   //Function to convert the entered value in UI into upper-case
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
   //Custom Auto Complete Function.
	app.directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
    };
});
		
		
//Custom Filter Function to sort the record.
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
        
        /* 
 function AddCustCtrl($scope, $http, $location,$window) 
   {
      $scope.master = {};
      $scope.activePath = null;
      $scope.add_new = function(newordertranmast, AddNewForm) {
       //console.log(newordertranmast);
	   //$window.alert("Edit Order Details  "+order.customername+" "+order.orderdate);
       // $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';   
		
var sql = 'tran_order_insertdata.php?'+'orderdate='+newordertranmast.orderdate+'&refnumber='+newordertranmast.refnumber+'&customername='+newordertranmast.customername +'&contactnumber='+newordertranmast.contactnumber+'&deliverydate='+newordertranmast.deliverydate+'&soldby='+newordertranmast.soldby+'&deliveredby='+newordertranmast.deliveredby+'&glassfrom='+newordertranmast.glassfrom+'&note='+newordertranmast.note+'&remarks='+newordertranmast.remarks+'&grandtotal='+newordertranmast.grandtotal+'&advreceived='+newordertranmast.advreceived;
$window.alert(sql);

return;

        $http.post(sql).success(function()
          {
            $window.alert( 'Record successfully inserted');
				window.location.href = 'tran_order_ajs.php#/editorder/';
           });
          
      
        $scope.reset = function() 
        {
           $scope.newordertranmast = angular.copy($scope.master);
        };
        $scope.reset();
      };
    }


ctrl.directive('handleDestroy', function() {
    return function(scope, tElement, attributes) {        
        scope.$on('$destroy', function() {
            alert("In destroy of:" + scope.todo.text);
        });
    };
});
 */
